<?php
/* This file is part of a copyrighted work; it is distributed with NO WARRANTY.
 * See the file COPYRIGHT.html for more details.
 */
 
  require_once("../shared/common.php");
  $tab = "school";
  $restrictToMbrAuth = TRUE;
  $nav = "edit_school";
  $restrictInDemo = true;
  require_once("../shared/logincheck.php");

  require_once("../classes/School.php");
  require_once("../classes/SchoolQuery.php");
  require_once("../classes/DmQuery.php");
  require_once("../functions/errorFuncs.php");
  require_once("../classes/Localize.php");
  $loc = new Localize(OBIB_LOCALE,$tab);

  #****************************************************************************
  #*  Checking for post vars.  Go back to form if none found.
  #****************************************************************************

  if (count($_POST) == 0) {
    header("Location: ../school/index.php");
    exit();
  }

  #****************************************************************************
  #*  Validate data
  #****************************************************************************
  $schoolid = $_POST["schoolid"];

  $school = new School();
  $school->setAddress($_POST["address"]);
  $_POST["address"] = $school->getAddress();
  $school->setAnexee($_POST["anexee"]);
  $_POST["anexee"] = $school->getAnexee();
  $school->setAreaId($_POST["areaid"]);
  $_POST["areaid"] = $school->getAreaId();
  $school->setCityId($_POST["cityid"]);
  $_POST["cityid"] = $school->getCityId();
  $school->setCue($_POST["cue"]);
  $_POST["cue"] = $school->getCue();
  $school->setDepartamentId($_POST["departamentid"]);
  $_POST["departamentid"] = $school->getDepartamentId();
  $school->setEst($_POST["est"]);
  $_POST["est"] = $school->getEst();
  $school->setName($_POST["name"]);
  $_POST["name"] = $school->getName();
  $school->setPhone($_POST["phone"]);
  $_POST["phone"] = $school->getPhone();
  $school->setStateId($_POST["stateid"]);
  $_POST["stateid"] = $school->getStateId();
  $school->setSchoolId($_POST["schoolid"]);
  $_POST["schoolid"] = $school->getSchoolId();  
  
  $validData = $school->validateData();
  if (!$validData) {
    $pageErrors["cue"] = $school->getCueError();
    $pageErrors["name"] = $school->getNameError();
    $_SESSION["postVars"] = $_POST;
    $_SESSION["pageErrors"] = $pageErrors;
    header("Location: ../school/school_edit_form.php");
    exit();
  }

  #**************************************************************************
  #*  Check for duplicate barcode number
  #**************************************************************************
  $schoolQ = new SchoolQuery();
  $schoolQ->connect();
  $dupCue = $schoolQ->DupCue($school->getCue(),$school->getSchoolId());
  if ($dupCue) {
    $pageErrors["cue"] = $loc->getText("schoolDupCue",array("cue"=>$school->getCue()));
    $_SESSION["postVars"] = $_POST;
    $_SESSION["pageErrors"] = $pageErrors;
    header("Location: ../school/school_edit_form.php");
    exit();
  }

  #**************************************************************************
  #*  Update library member
  #**************************************************************************
  $schoolQ->update($school);
  $schoolQ->close();

  #**************************************************************************
  #*  Destroy form values and errors
  #**************************************************************************
  unset($_SESSION["postVars"]);
  unset($_SESSION["pageErrors"]);

  $msg = $loc->getText("schoolEditSuccess");
  header("Location: ../school/school_view.php?schoolid=".U($school->getSchoolId())."&reset=Y&msg=".U($msg));
  exit();
?>
